import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA
 * Description 洗牌 抓牌
 * User: 王杰
 * Date: 2025-05-24
 * Time: 20:21
 */
public class CartDemo {
    private static final String[] suits = {"❤", "♠", "♣", "♦"};

    // 买牌
    public List<Cart> buyCard() {
        List<Cart> cartList = new ArrayList<>();
        for (int i = 1; i <= 13; i++) {
            for (int j = 0; j < 4; j++) {
                int rank = i;
                String suit = suits[j];
                Cart cart = new Cart(suit, rank);
                cartList.add(cart);
            }
        }
        return cartList;
    }

    // 洗牌
    public void shuffle(List<Cart> cartList) {
        Random random = new Random();
        for (int i = cartList.size() - 1; i > 0; i--) {
            int index = random.nextInt(cartList.size());
            swap(cartList, i, index);
        }
    }

    private void swap(List<Cart> cartList, int i, int j) {
        Cart tmp = cartList.get(i);
        cartList.set(i, cartList.get(j));
        cartList.set(j, tmp);
    }


    public List<List<Cart>> play(List<Cart> cartList) {
        List<Cart> hand0 = new ArrayList<>();
        List<Cart> hand1 = new ArrayList<>();
        List<Cart> hand2 = new ArrayList<>();

        List<List<Cart>> hand = new ArrayList<>();
        hand.add(hand0);
        hand.add(hand1);
        hand.add(hand2);

        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                Cart cart = cartList.remove(0);
                hand.get(j).add(cart);
            }
        }

        return hand;
    }
}
